package com.zyd.blog.personal.service.impl;

import com.zyd.blog.personal.enuminterface.Status;
import com.zyd.blog.persistence.mapper.UserThumbMapper;
import com.zyd.blog.personal.service.UserThumbService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户点赞业务逻辑层
 * @author mfy
 */
@Service
public class UserThumbServiceImpl implements UserThumbService {

    @Resource
    private UserThumbMapper userThumbMapper;

    @Override
    public Boolean isHaveThumb(Long id ,Long userId, Long productionId,String status){
        Map<String,Object> condition = new HashMap<>(16);
        condition.put("id",id);
        condition.put("userId",userId);
        condition.put("productionId",productionId);
        condition.put("status",status);
        List<Map<String,Object>> thumb = userThumbMapper.queryUserThumbByCondition(condition);
        return thumb != null && thumb.size() != 0;
    }

    @Override
    public List<Map<String,Object>> getThumbInfoByCondition(Long userId, Long productionId,String status){
        Map<String,Object> condition = new HashMap<>(16);
        condition.put("userId",userId);
        condition.put("productionId",productionId);
        condition.put("status",status);
        return userThumbMapper.queryUserThumbByCondition(condition);
    }

    @Override
    public Map<String,Object> getThumbInfo(Long userId,Long productionId){
        Map<String,Object> condition = new HashMap<>(16);
        condition.put("userId",userId);
        condition.put("productionId",productionId);
        List<Map<String,Object>> thumbs = userThumbMapper.queryUserThumbByCondition(condition);
        Map<String,Object> result = new HashMap<>(16);
        result.put("userId",userId);
        result.put("productionId",productionId);
        if(thumbs == null || thumbs.size() == 0){
            result.put("thumb", Status.STATUS_NO);
            //  定位是哪种没点赞情况,此处为点赞表无数据
            result.put("status",Status.STATUS_NO);
        }else{
            result.put("thumb",thumbs.get(0).get("status"));
            result.put("status",Status.STATUS_YES);
        }
        return result;
    }

    @Override
    public Object thumb(Long userId,Long productionId,String status){
        Map<String,Object> thumb = getThumbInfo(userId,productionId);
        String statusC = "status";
        String thumbC = "thumb";
        switch (status){
            case Status.STATUS_NO :
                if(Status.STATUS_NO.equals(thumb.get(statusC))){
                    //  并未点赞,无需取消
                    return false;
                }else{
                    if(Status.STATUS_NO.equals(thumb.get(thumbC))){
                        //  并未点赞,无需取消
                        return false;
                    }else{
                        thumb.put("status",Status.STATUS_NO);
                        thumb.put("updateTime",(new Date()).getTime());
                        return userThumbMapper.updateUserThumb(thumb) > 0;
                    }
                }
            case Status.STATUS_YES :
                if(Status.STATUS_NO.equals(thumb.get(statusC))){
                    thumb.put("updateTime",(new Date()).getTime());
                    thumb.put("addTime",(new Date()).getTime());
                    return userThumbMapper.addUserThumb(thumb) > 0;
                }else{
                    if(Status.STATUS_NO.equals(thumb.get(thumbC))){
                        thumb.put("status",Status.STATUS_YES);
                        thumb.put("updateTime",(new Date()).getTime());
                        return userThumbMapper.updateUserThumb(thumb) > 0;
                    }else{
                        //  已点赞
                        return false;
                    }
                }
            default:
                break;
        }
        return false;
    }
}
